package com.msh.automatic.android.control.util

import com.alibaba.fastjson2.JSONObject
import com.msh.automatic.android.control.task.Task
import com.msh.automatic.android.control.task.TaskLog
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow

class EventUtil {
    object EventBus {
        private val _events = MutableSharedFlow<Message>(
            replay = 0,
            extraBufferCapacity = 1,  // 增加缓冲容量
            onBufferOverflow = BufferOverflow.DROP_OLDEST
        )
        val events = _events.asSharedFlow()

        suspend fun post(event: Message) {
            _events.emit(event)
        }

        fun tryPost(event: Message): Boolean {
            return _events.tryEmit(event)
        }
    }
    sealed class Message {
        data class MqttMessage(
            var mqttId:Int,
            var mqttTopic:String,
            var mqttQos:Int,
            var mqttPayload:String,
            var task:Task,
            var taskLog: TaskLog,
        ) : Message()
    }
}
enum class MqttMessageType{
    task
}
